#include "std_atomic.h"

CounterAtomic g_counter;

std::once_flag flag;

void do_once() {
    std::call_once(flag, []() {
       std::cout << "called once" << std::endl;
    });
}

void increments() {
	for (int i = 0; i < 10; ++i) {
		g_counter.increment();
		std::cout << g_counter.get() << std::endl;
	}
}

void decrements() {
	for (int i = 0; i < 5; ++i) {
		g_counter.decrement();
		std::cout << g_counter.get() << std::endl;
	}
}

int std_atomic () {
    std::thread t1(increments);
	std::thread t2(decrements);

	t1.join();
	t2.join();


    //! TODO: std::atomic支持自定义类型


    //! call once
    std::thread t3(do_once);
    std::thread t4(do_once);
    std::thread t5(do_once);

    t3.join();
    t4.join();
    t5.join();

    return 0;
}